import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { DICT_TYPE } from '@vben/constants';

import { z } from '#/adapter/form';
import { getSimpleDeviceGroupList } from '#/api/iot/device/group';
import { getDictOptions } from '@vben/hooks';

import { $t } from '#/locales';

/** 新增/修改设备分组的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      component: 'Input',
      fieldName: 'id',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'name',
      label: $t('views.iot.device.group.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.iot.device.group.namePlaceholder'),
      },
      rules: z
        .string()
        .min(1, $t('views.iot.device.group.nameRequired'))
        .max(64, $t('views.iot.device.group.nameMaxLength')),
    },
    {
      fieldName: 'parentId',
      label: $t('views.iot.device.group.parentId'),
      component: 'ApiTreeSelect',
      componentProps: {
        api: getSimpleDeviceGroupList,
        fieldNames: {
          label: 'name',
          value: 'id',
        },
        placeholder: $t('views.iot.device.group.parentIdPlaceholder'),
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'RadioGroup',
      defaultValue: 1,
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
      },
      rules: 'required',
    },
    {
      fieldName: 'description',
      label: $t('views.iot.device.group.description'),
      component: 'Textarea',
      componentProps: {
        placeholder: $t('views.iot.device.group.descriptionPlaceholder'),
        rows: 3,
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.iot.device.group.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.iot.device.group.namePlaceholder'),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    {
      field: 'name',
      title: $t('views.iot.device.group.name'),
      minWidth: 200,
      treeNode: true,
    },
    {
      field: 'description',
      title: $t('views.iot.device.group.description'),
      minWidth: 200,
    },
    {
      field: 'status',
      title: $t('common.status'),
      width: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'deviceCount',
      title: $t('views.iot.device.group.deviceCount'),
      minWidth: 100,
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
